﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace psafth.P3DecoderV2
{
    public class TransponderPassing : BackgroundWorker
    {
        public TransponderPassing()
        {
            this.WorkerReportsProgress = true;
            this.WorkerSupportsCancellation = true;
        }

        public int TransponderId
        {
            get;
            set;
        }

        protected override void OnDoWork(DoWorkEventArgs e)
        {
            Random rand = new Random((int)DateTime.Now.Ticks);

            int startSeconds = rand.Next(1, 10);

            TimeSpan startTime = new TimeSpan(0, 0, 0, startSeconds, (int)rand.NextDouble() * 1000);

            Thread.Sleep((int)Math.Ceiling(startTime.TotalMilliseconds));

            while (true)
            {
                if (this.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }

                Random r = new Random((int)DateTime.Now.Ticks);

                int time = r.Next(14, 24);

                TimeSpan t = new TimeSpan(0, 0, 0, time, (int)r.NextDouble() * 1000);

                try
                {
                    Thread.Sleep((int)Math.Ceiling(t.TotalMilliseconds));

                    byte[] message = Commands.Passing(this.TransponderId, DateTime.Now);

                    TransferObject obj = new TransferObject("Passing", this.TransponderId.ToString(), message);

                    ReportProgress(0, obj);
                }
                catch (Exception)
                {
                }
            }
        }
    }
}
